local weimeng = fk.CreateSkill {

  name = "joy__weimeng",

  tags = {  },

}



weimeng:addEffect("active", {
  name = "joy__weimeng",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = function(self, player, selected, selected_cards)
    local n = player:getMark("@@"..weimeng.name) > 0 and 1 or 0
    return "#joy__weimeng:::"..(player.hp + n)
  end,
  can_use = function(self, player)
    local n = player:getMark("@@"..weimeng.name) > 0 and 1 or 0
    return player:usedSkillTimes(weimeng.name, Player.HistoryPhase) == 0 and (player.hp + n) > 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local n = player:getMark("@@"..weimeng.name) > 0 and 1 or 0
    local cards = room:askForCardsChosen(player, target, 1, player.hp + n, "h", weimeng.name)
    room:setPlayerMark(player,"@@"..weimeng.name,0)
    local n1 = 0
    for _, id in ipairs(cards) do
      n1 = n1 + Fk:getCardById(id).number
    end
    room:obtainCard(player, cards, false, fk.ReasonPrey)
    if player.dead or player:isNude() or target.dead then return end
    local cards2
    if #player:getCardIds("he") <= #cards then
      cards2 = player:getCardIds("he")
    else
      cards2 = room:askForCard(player, #cards, #cards, true, weimeng.name, false, ".",
        "#joy__weimeng-give::"..target.id..":"..#cards..":"..n1)
      if #cards2 < #cards then
        cards2 = table.random(player:getCardIds("he"), #cards)
      end
    end
    local n2 = 0
    for _, id in ipairs(cards2) do
      n2 = n2 + Fk:getCardById(id).number
    end
    room:obtainCard(target, cards2, false, fk.ReasonGive, player.id)
    if n1 < n2 then
      if not player.dead then
        player:drawCards(1, weimeng.name)
      end
    elseif n1 > n2 then
      if not (player.dead or target.dead or target:isAllNude()) then
        local id = room:askForCardChosen(player, target, "hej", weimeng.name)
        room:throwCard({id}, weimeng.name, target, player)
      end
    elseif n1 == n2 then
      room:setPlayerMark(player,"@@"..weimeng.name,1)
    end
  end,
})

return weimeng